/**
 * TmCustomAlertAction.java
 * Created at 2017-10-30
 * Created by zhanghailin
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.system.action;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.utils.StringUtils;
import com.broadtext.common.utils.UUIDUtils;
import com.broadtext.rms.system.model.TmCustomAlert;
import com.broadtext.rms.system.service.ITmCustomAlertService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>ClassName: TmCustomAlertAction</p>
 * <p>Description: 自定义告警控制层</p>
 * <p>Author: zhanghailin</p>
 * <p>Date: 2017-10-30</p>
 */
@Controller
@RequestMapping("/main/customAlert")
public class TmCustomAlertAction {

    /**
     * 日志信息
     */
    public static Logger logger = LoggerFactory.getLogger(TmCustomAlertAction.class);
    
    /**
     * 自定义告警服务类
     */
    @Autowired
    @Qualifier("tmCustomAlertService")
    private ITmCustomAlertService tmCustomAlertService;
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param request
     * @param response
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/getCustomAlert.mvc")
    @ResponseBody
    public Map<String,Object> getCustomAlert(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
        PageBounds pageBounds = new PageBounds();
        //解析请求参数，将String字符串转为map
        Map<String,Object> map = new HashMap<String,Object>();
        String aoData = request.getParameter("aoData");
        JSONArray array = JSONArray.parseArray(aoData);
        if (array!=null) {
            Iterator<Object> it = array.iterator();
            while (it.hasNext()) {
                JSONObject obj;
                obj = (JSONObject) it.next();
                if (obj.get("value") != null && StringUtils.isNotBlank(obj.get("value")+"")) {
                    map.put(obj.get("name") + "", obj.get("value"));
                }
            }
        }
        //分页查询条数
        pageBounds.setLimit(Integer.parseInt(map.get("iDisplayLength").toString()));
        //分页第几页
        pageBounds.setPage(Integer.parseInt(map.get("iDisplayStart").toString()) / pageBounds.getLimit() + 1);
        Map<String, Object> dataMap = new HashMap<String, Object>();
        try {
            EntityPageBean<TmCustomAlert> pageBean = this.tmCustomAlertService.findEntityPageByQuery(map, pageBounds);
            dataMap.put("recordsTotal", pageBean.getiTotalRecords());
            dataMap.put("recordsFiltered", pageBean.getiTotalRecords());
            dataMap.put("aaData", pageBean.getResults());
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return dataMap;
    }
    
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param request
     * @param response
     * @param TmCustomAlert
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/saveOrUpdateCustomAlert.mvc")
    @ResponseBody
    public Map<String, Object> saveOrUpdateCustomAlert(HttpServletRequest request, HttpServletResponse response,TmCustomAlert t) throws ServiceException {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        String currentUser = String.valueOf(request.getSession().getAttribute("sys.userName"));
        t.setDelFlag("0");
        t.setUpdateUser(currentUser);
        t.setUpdateDate(new Date());
        try{
            if (!"".equals(t.getPkId())) {
                //编辑
                this.tmCustomAlertService.updateCustomAlert(t);
            } else {
                //新增
                t.setCreateUser(currentUser);
                t.setCreateDate(new Date());
                t.setPkId(UUIDUtils.getUUID());
                this.tmCustomAlertService.saveCustomAlert(t);
            }
            dataMap.put("msg", "SUCCESS");
        } catch (ServiceException e) {
            dataMap.put("msg", "FAILURE");
            e.printStackTrace();
        }
        return dataMap;
    }
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param request
     * @param response
     * @param TmCustomAlert
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/deleteCustomAlert.mvc")
    @ResponseBody
    public Map<String, Object> deleteCustomAlert(HttpServletRequest request, HttpServletResponse response,String pkId) throws ServiceException {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        //数组 存储待删除主键id
        String[] idArray;
        idArray = pkId.split(",");
        try{
            this.tmCustomAlertService.delete(idArray, String.valueOf(request.getSession().getAttribute("sys.userName")));
            dataMap.put("msg", "SUCCESS");
        } catch (ServiceException e) {
            dataMap.put("msg", "FAILURE");
            e.printStackTrace();
        }
        return dataMap;
    }
    
    /**
     * 
     * <p>
     * Description: 根据id获取编辑对象
     * </p>
     * 
     * @param id 主键id
     * @return 客户对象
     * @throws ServiceException 异常
     */
    @RequestMapping(value = "/getCustomAlertById.mvc")
    @ResponseBody
    public TmCustomAlert getCustomerById(@RequestParam("pkId") String id) throws ServiceException {
        return this.tmCustomAlertService.findEntityById(id);
    }
    
    /**
     * 
     * <p>
     * Description: 检查告警编码是否重复
     * </p>
     * 
     * @param request
     * @param response
     * @return Map<String, Object>
     * @throws ServiceException 异常
     */
    @RequestMapping(value = "/checkRepeatCode.mvc")
    @ResponseBody
    public Map<String, Object>  checkRepeatCode(String alertCode, String pkId) throws ServiceException {
        Map<String, Object> map = new HashMap<String, Object>();
        Boolean flag = this.tmCustomAlertService.checkRepeatCode(alertCode, pkId);
        if(flag){
            map.put("msg", true);
        }else {
            map.put("msg", false);
        }
        return map;
    }
    
    
    
}
